
import BaseView from './BaseView'

import loader from '../loader'
import _ from 'underscore'

var ModuleView = BaseView.extend({
  type: "ModuleView",

  willRender: function () {


  this.views = _.chain(this.options.views || [])
          .sortBy('index')
          .map(function(each) {
            return this.createView(each);
          }.bind(this))
          .compact()
          .value();

  },

  createView: function (each) {
    if (each.type == 'html') {
      //the html should wrap outermost element
      each.el = $(loader.getTemplate(each.template))[0];
      return each;
    } else {
      var View = loader.getPage(each.type).default;
      // 加载不存在的文件直接报错
      //if (!View) throw new Error('View ' + each.type + ' Not Exists.');
      var view = new View(each);
      // view.el.setAttribute('data-type', each.type);
      return view;
    }
  },
  // set
  render: function () {
    this.views.reduce(function(el, view) {
      el.appendChild(view.el)
      return el;
    }, this.el);
    return this;

  },

})


export default ModuleView
